#!/bin/sh
cd ${0%/*} || exit 1    # Run from this directory

# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions

gYs="air.gas H2O.gas C3H8O.gas"
lYs="H2O.liquid C3H8O.liquid"

runApplication zeroDimensionalMesh

setInertY()
{
    y=${1%.*}
    phase=${1#*.}
    [ $y != "none" ] && arg="-set $y" || arg="-remove"
    runApplication -a foamDictionary -entry defaultSpecie $arg \
        constant/physicalProperties.$phase
}

for gY in $gYs
do
    setInertY $gY
    for lY in $lYs
    do
        setInertY $lY
        runApplication -s ${gY}_${lY} foamRun
        mv postProcessing postProcessing_${gY}_$lY
    done
done

line()
{
    path=plot/0/volFieldValue.dat

    index=$(awk -v RS="\t" "/volAverage\($1\)/{print NR; exit}" \
        postProcessing_${gYs%% *}_${lYs%% *}/$path)

    cat << EOF
    'postProcessing_${gYs%% *}_${lYs%% *}/$path' \
    us 1:$index w l axes $2 lc $3 t '$4', \
    for [gY in '$gYs'] for [lY in '$lYs'] \
    'postProcessing_'.gY.'_'.lY.'/$path' \
    us 1:$index w l axes $2 lc $3 notitle
EOF
}

gnuplot << EOF

set terminal eps enhanced size 5.83,8.27
set output 'postProcessing.eps'

set lmargin at screen 0.15
set rmargin at screen 0.84

set multiplot layout 6,1

set xlabel "Time (s)"

set ytics nomirror
set y2tics
set ylabel 'Gas volume fraction'
set y2label 'Liquid volume fraction'

plot \
    $(line alpha.gas x1y1 1 Gas), \
    $(line alpha.liquid x1y2 2 Liquid)

set ytics mirror
unset y2tics
set ylabel 'Temperature (K)'
unset y2label

plot \
    $(line T.gas x1y1 1 Gas), \
    $(line T.liquid x1y1 2 Liquid), \
    $(line phaseChange:Ts x1y1 3 Interface)

set ytics nomirror
set y2tics
set ylabel "Vapour mass fraction"
set y2label "Air mass fraction"

plot \
    $(line H2O.gas x1y1 1 H2O), \
    $(line C3H8O.gas x1y1 2 C3H8O), \
    $(line air.gas x1y2 3 Air)

set ytics mirror
unset y2tics
set ylabel "Liquid mass fraction"
unset y2label

plot \
    $(line H2O.liquid x1y1 1 H2O), \
    $(line C3H8O.liquid x1y1 2 C3H8O)

set ytics nomirror
set y2tics
set ylabel "Mass (kg/m^3)"
set y2label "Energy (J/m^3)"

plot \
    $(line dMass.gas x1y1 1 "Gas Mass Change"), \
    $(line dMass.liquid x1y1 2 "Liquid Mass Change"), \
    $(line dEnergy.gas x1y2 3 "Gas Energy Change"), \
    $(line dEnergy.liquid x1y2 4 "Liquid Energy Change")

set ytics nomirror
set y2tics
set ylabel "Mass (kg/m^3)"
set y2label "Energy (J/m^3)"

plot \
    $(line dMass x1y1 1 "Mass Error"), \
    $(line dEnergy x1y2 2 "Energy Error")

unset multiplot

EOF

#------------------------------------------------------------------------------
